package com.example.system_development.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.system_development.common.R;
import com.example.system_development.entity.Product;
import com.example.system_development.service.ProductService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 新增产品信息
     * @param request
     * @param product
     * @return
     */
    @ApiOperation(value = "新增产品")
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Product product) {
        //获取当前用户name，将其设置为admin_name
        String userName = (String) request.getSession().getAttribute("userName");
        product.setAdminName(userName);

        //设置当前时间为上传时间
        product.setUpdateDate(LocalDate.now());

        productService.save(product);
        return R.success("生成产品二维码成功");
    }

    /**
     * 根据id查询产品信息
     * @param productId
     * @return
     */
    @ApiOperation(value = "查询产品信息")
    @GetMapping("/{productId}")
    public R<Product> getById(@PathVariable Integer productId) {
        Product product = productService.getById(productId);
        if (product != null) {
            return R.success(product);
        }
        return R.error("没有查到对应产品信息");
    }

    /**
     * 产品信息分页查询
     * @param page
     * @param pageSize
     * @param productName
     * @param batch
     * @param produceDate
     * @param notes
     * @return
     */
    @ApiOperation(value = "产品信息分页查询")
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String productName, String batch, String produceDate, String notes) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(productName), Product::getProductName, productName)
                .or()
                .like(StringUtils.isNotEmpty(batch), Product::getBatch, batch)
                .or()
                .like(StringUtils.isNotEmpty(produceDate), Product::getProduceDate, produceDate)
                .or()
                .like(StringUtils.isNotEmpty(notes), Product::getNotes, notes);
        queryWrapper.orderByAsc(Product::getProductId);
        productService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 删除产品
     * @param productId
     * @return
     */
    @ApiOperation(value = "删除产品")
    @DeleteMapping
    public R<String> delete(Integer productId) {
        productService.removeById(productId);
        return R.success("删除产品成功");
    }
}
